package com.app.shop.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.app.shop.R;
import com.app.shop.activitys.DTDetailActivity;
import com.app.shop.activitys.DTEditActivity;
import com.app.shop.adapters.DTListAdapter;
import com.app.shop.beans.DTBean;
import com.app.library.base.BaseFragment;
import com.app.library.beans.EventMessage;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 动态
 */
public class DTFragment extends BaseFragment implements View.OnClickListener{


    private RecyclerView recy;
    private     TextView tvadd;
    private List<DTBean> list = new ArrayList<>();
    private DTListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_dt, container, false);
        recy= (RecyclerView)view.findViewById(R.id.recy);
        tvadd = view.findViewById(R.id.tv_add);
        tvadd.setOnClickListener(this::onClick);
        registerEventBus();
        initList();
        return view;
    }
    public static DTFragment getInstance(){
        DTFragment fragment=new DTFragment();
        return fragment;
    }
    //列表
    private void initList() {

        list = DataSupport.findAll(DTBean.class);
        Collections.reverse(list);

        adapter = new DTListAdapter(R.layout.item_dt_list, list);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_list_empty, null);
        TextView tv_empty = view.findViewById(R.id.tv_empty_title);
        tv_empty.setText("暂无动态，等待用户发布");
        adapter.setEmptyView(view);
        recy.setLayoutManager(new LinearLayoutManager(getActivity()));
        recy.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", list.get(position));
                skipActivity(getActivity(), DTDetailActivity.class, bundle);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_del_item) {
                    DataSupport.deleteAll(DTBean.class, "d_id=?", list.get(position).d_id);
                    list.remove(position);
                }
                adapter.notifyDataSetChanged();
            }
        });

    }




    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            initList();
        }
    }

    @Override
    public void onClick(View v) {
        skipActivity(getActivity(), DTEditActivity.class);
    }
}
